import {
  createRouter,
  createWebHashHistory
} from 'vue-router'

const routes = [
  {
    name: 'Error404',
    path: '/:error*',
    component: () => import('@/pages/error/404.vue'),
    meta: {
      isLogin: false
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/pages/login/index.vue'),
    meta: {
      isLogin: false
    }
  },
  {
    path: '/',
    redirect: '/overview',
    component: () => import('@/pages/index/index.vue'),
    children: [
      {
        path: '/overview',
        name: 'Overview',
        component: () => import('@/pages/overview/index.vue')
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const toPath = to.path
  const token = localStorage.getItem('_cangku_token')
  if (toPath === '/login') {
    if (token) {
      next({ name: 'Overview' })
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next({ name: 'Login' })
    }
  }
})
export default router
export {
  routes
}
